<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\common\Common;


class AdminController extends Controller
{
    public $layout = 'admin';
    public $enableCsrfValidation = false;//不验证csrf

    public function actionIndex()
    {
        return $this->render('index');
    }

    //====================  分类管理  ===========================================================================

    public function actionCategory()
    {
        return $this->render('category');
    }

    public function actionCategoryTree()
    {
        $category1 = $this->getCategory1();
        if ($category1 && is_array($category1)) {
            foreach ($category1 as &$category1Item) {
                $category2                 = $this->getCategory2($category1Item['id']);
                $category1Item['children'] = $category2;
            }
        }
        Common::echoJson('success', '获取成功', $category1);
    }

    public function getCategory1()
    {
        $sql    = "
            select `id`,`name` as title,`pid`,`type` from `category` where `type` = 0 and `is_delete` = 0
        ";
        $result = Yii::$app->db->createCommand($sql)->queryAll();
        if (!$result) {
            return array();
        }
        return $result;
    }

    public function getCategory2($pid)
    {
        $sql    = "
            select `id`,`name` as title,`pid`,`type` from `category` where `pid` = {$pid} and `is_delete` = 0
        ";
        $result = Yii::$app->db->createCommand($sql)->queryAll();
        if (!$result) {
            return array();
        }
        return $result;
    }

    public function actionDelCategory()
    {
        $id     = Common::getParam('id');
        $sql    = "
            update `category` set `is_delete` = 1 where `id` = {$id}
        ";
        $result = Yii::$app->db->createCommand($sql)->execute();
        if (!$result) {
            Common::echoJson('error', '删除失败');
        }
        Common::echoJson('success', '删除成功');
    }

    public function actionAddCategory()
    {
        $pid    = Common::getParam('pid');
        $name   = Common::getParam('name');
        $sql    = "
            insert into `category` (`name`,`pid`,`type`) values ('{$name}',{$pid},1)
        ";
        $result = Yii::$app->db->createCommand($sql)->execute();
        if (!$result) {
            Common::echoJson('error', '添加失败');
        }
        Common::echoJson('success', '添加成功');
    }

    public function actionEditCategory()
    {
        $id     = Common::getParam('id');
        $name   = Common::getParam('name');
        $sql    = "
            update `category` set `name` = '{$name}' where `id` = {$id}
        ";
        $result = Yii::$app->db->createCommand($sql)->execute();
        if (!$result) {
            Common::echoJson('error', '编辑失败');
        }
        Common::echoJson('success', '编辑成功');
    }

    //====================  图书管理  ===========================================================================
    public function actionBook()
    {
        return $this->render('book');
    }

    public function actionBookList()
    {
        $sql    = "
            select `id`,`name`,`path`,`category1`,`category2` from `books`
        ";
        $result = Yii::$app->db->createCommand($sql)->queryAll();
        if ($result) {
            die(json_encode(['code' => 0, 'msg' => '获取成功', 'count' => $this->bookCount(), 'data' => $result]));
        }
    }

    public function bookCount()
    {
        $sql   = "
            select count(`id`) count from `books`
        ";
        $count = Yii::$app->db->createCommand($sql)->queryOne();
        return $count['count'];
    }

    public function actionAddBook()
    {
        $category1 = $this->getCategory1();
        return $this->render('addBook', ['category1' => $category1]);
    }

    public function actionGetCategory2()
    {
        $pid       = Common::getParam('pid');
        $category2 = $this->getCategory2($pid);
        Common::echoJson('success', '获取成功', $category2);
    }

    public function actionBookAdd()
    {
        $name = Common::getParam('name');
        $path = Common::getParam('path');
        $cid1 = Common::getParam('cid1');
        $cid2 = Common::getParam('cid2');
        $time = time();

        $sql    = "
            insert into `books` (`name`,`path`,`category1`,`category2`,`create_time`,`update_time`)
            values ('{$name}','{$path}',{$cid1},{$cid2},{$time},{$time})
        ";
        $result = Yii::$app->db->createCommand($sql)->execute();
        if (!$result) {
            Common::echoJson('error', '添加失败');
        }
        Common::echoJson('success', '添加成功');
    }

    public function actionBookDel()
    {
        $id     = Common::getParam('id');
        $sql    = "
            delete from `books` where `id` = {$id}
        ";
        $result = Yii::$app->db->createCommand($sql)->execute();
        if (!$result) {
            Common::echoJson('error', '删除失败');
        }
        Common::echoJson('success', '删除成功');
    }
}
